import 'package:aitao_flutter/common_ui/back_nav.dart';
import 'package:aitao_flutter/common_ui/confirm_btn.dart';
import 'package:aitao_flutter/model/share_model.dart';
import 'package:aitao_flutter/routes/routes.dart';
import 'package:aitao_flutter/style/box_style.dart';
import 'package:aitao_flutter/style/color_style.dart';
import 'package:aitao_flutter/style/edgeInsets_style.dart';
import 'package:aitao_flutter/style/radius_style.dart';
import 'package:aitao_flutter/style/text_style.dart';
import 'package:aitao_flutter/utils/alert_utils.dart';
import 'package:aitao_flutter/utils/wx_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../request/api.dart';
import '../../request/requestManager.dart';
import '../../utils/log_utils.dart';

class InvitePage extends StatefulWidget {
  const InvitePage({Key? key}) : super(key: key);

  @override
  State<InvitePage> createState() => _InvitePageState();
}

class _InvitePageState extends State<InvitePage> {
  List<String> reward = [];
  String title = '邀请好友赚大钱';
  int totalReward = 0; // 累计奖励
  int totalInvite = 0; // 累计邀请
  InviteShareModel? wechatShare;
  InviteShareModel? qqShare;
  InviteShareModel? share;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BackNavigation(
        title: '邀请有奖',
      ),
      body: Container(
        padding: EdgeInsetsStyle.edgeInsets(24, 15),
        decoration:
            const BoxDecoration(gradient: ColorStyle.inviteBackgroundColor),
        child: CustomScrollView(
          slivers: [_inviteTitleWidget(), _inviteDesWidget()],
        ),
      ),
    );
  }

  Widget _inviteDesWidget() {
    return SliverToBoxAdapter(
      child: Container(
          padding: EdgeInsetsStyle.customMargin(20, 24, 20, 40),
          decoration: BoxDecoration(
              color: ColorStyle.whiteColor,
              borderRadius: RadiusStyle.radiusAll(10)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              // 会员奖励描述
              Column(
                  children: reward.map((e) {
                return Padding(
                    padding: const EdgeInsets.only(bottom: 12),
                    child: Text(
                      e.replaceAll('#', ''),
                      style: TextStyle(
                          color: ColorStyle.setColor(0xFFA925E7), fontSize: 14),
                    ));
              }).toList()),
              Box.vBox15,
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    children: [
                      Text(
                        '累计奖励',
                        style: TextStyle(
                            color: ColorStyle.setColor(0xFFD61A15),
                            fontSize: 18),
                      ),
                      Box.vBox5,
                      Text(
                        '$totalReward',
                        style: TextStyle(
                            color: ColorStyle.setColor(0xFFD61A15),
                            fontSize: 18),
                      ),
                    ],
                  ),
                  Column(
                    children: [
                      Text(
                        '累计邀请',
                        style: TextStyle(
                            color: ColorStyle.setColor(0xFFD61A15),
                            fontSize: 18),
                      ),
                      Box.vBox5,
                      Text(
                        '$totalInvite人',
                        style: TextStyle(
                            color: ColorStyle.setColor(0xFFD61A15),
                            fontSize: 18),
                      ),
                    ],
                  )
                ],
              ),
              Box.vBox30,
              GestureDetector(
                child: Text(
                  '我邀请的人',
                  style: TextStyle(
                      color: ColorStyle.setColor(0xFFAC2DE8), fontSize: 18),
                ),
                onTap: () {
                  Get.toNamed(Routes.inviteListPage);
                },
              ),
              Container(
                padding: EdgeInsetsStyle.edgeInsets(52, 25),
                child: ConfirmButton(
                  titleColor: ColorStyle.whiteColor,
                  title: '立即分享',
                  backGroundColor: ColorStyle.setColor(0xFFFF3C3C),
                  callBack: () {
                    showShareSheet();
                  },
                ),
              )
            ],
          )),
    );
  }

  Widget _inviteTitleWidget() {
    return SliverToBoxAdapter(
      child: Container(
        padding: EdgeInsetsStyle.only(bottom: 11),
        child: Column(
          children: [
            Text(
              title,
              style: Styles.whiteText_30,
            ),
            Box.vBox10,
            Text(
              '邀请手册',
              style: Styles.whiteText_16,
            ),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    getInviteData();
  }

  void getInviteData() async {
    var result = await RequestManager().post(API.inviteRules);
    if (result != null && result['code'] == 0) {
      WJLogUtils.wjLog(result);
      title = result['data']['title'].toString();
      reward = result['data']['reward'].cast<String>();
      totalReward = result['data']['total']['total_reward'];
      totalInvite = result['data']['total']['total_invite'];
      wechatShare = InviteShareModel.fromJson(result['data']['wechat_share']);
      qqShare = InviteShareModel.fromJson(result['data']['qq_share']);
      share = InviteShareModel.fromJson(result['data']['qq_share']);
      setState(() {});
    }
  }

  void showShareSheet() {
    AlertUtils().showShareSheet(context, (t) async {
      debugPrint(t);
      if (t == '微信好友') {
        if (wechatShare == null) return;
        WxUtil.wxShareSessionWebPage(wechatShare!);
      } else if (t == '朋友圈') {
        if (wechatShare == null) return;
        WxUtil.wxShareTimelineWebPage(wechatShare!);
      } else if (t == '复制链接') {
        if (share == null) return;
        Clipboard.setData(ClipboardData(text: share!.url ?? '')).then((value) {
          EasyLoading.showToast('复制成功,快去分享吧!');
        });
      }
    });
  }
}
